package nl.infodation.emr.utility;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.Hibernate;

/**
 * Initial entity manager factory
 * @author success
 *
 */
public final class EntityManagerFactoryUtil {
	
	private static Map<String, EntityManagerFactory> lstEntityManagerFactory = 
			new HashMap<String, EntityManagerFactory>();
	
	private EntityManagerFactoryUtil() {}
	
	
	public static synchronized EntityManagerFactory getEntityManagerFactory(
			final String persistenceUnit) {
		if(null == lstEntityManagerFactory.get(persistenceUnit)) {
			lstEntityManagerFactory.put(persistenceUnit, 
					Persistence.createEntityManagerFactory(persistenceUnit));
		}
		return lstEntityManagerFactory.get(persistenceUnit);
	}
	
	/**
	 * initialize lazy object after entity manager closed 
	 * @param object
	 */
	public static void initializeLazyObject(final Object object) {
		if(object != null) {
			Hibernate.initialize(object);
		}
	}
}
